/*
 * ProductStockOT.java
 *
 * $Id: ProductStockOT.java,v 1.4 2009-10-07 12:10:30 mario Exp $
 */
package org.ceteca.explica.core.warehouse;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;

import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.partners.AddressOT;
import org.ceteca.explica.core.util.Converter;

/**
 * Store class to hold the data of a warehouse.
 * <br/>
 * Responsibilities:
 * <li>
 * 	<ul> Store of warehouse data for exchanging data among presentation, 
 * business and data access layers. </ul>
 * </li>.
 * <br/>
 * @author Mario García García <mario@ceteca.es>
 * Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <br/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <br/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class ProductStockOT extends BaseVO implements Serializable {
	/**
	 * Internal id of the stock.
	 */
	private long idStock;
	/**
	 * Last update stock's date.
	 */
	private Date lastUpdate;
	/**
	 * Quantity of products in stock.
	 */
	private BigDecimal stock;
	/**
	 * Lot of the stock.
	 */
	private LotOT lot;
	/**
	 * Warehouse of the stock.
	 */
	private WarehouseOT warehouse;
	/**
	 * Product of the stock.
	 */
	private ProductOT product;

	/**
	 * Version of the stock used by Hibernate for optimistic concurrency control.
	 */
	private Integer version = new Integer(0);
	
	private static final long serialVersionUID = 10000003;
	
	
	public long getIdStock() {
		return idStock;
	}
	public void setIdStock(long idStock) {
		this.idStock = idStock;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}
	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}
	public BigDecimal getStock() {
		return stock;
	}
	public void setStock(BigDecimal stock) {
		this.stock = stock;
	}
	public LotOT getLot() {
		return lot;
	}
	public void setLot(LotOT lot) {
		this.lot = lot;
	}
	public ProductOT getProduct() {
		return product;
	}
	public void setProduct(ProductOT product) {
		this.product = product;
	}
	public WarehouseOT getWarehouse() {
		return warehouse;
	}
	public void setWarehouse(WarehouseOT warehouse) {
		this.warehouse = warehouse;
	}
	public Integer getVersion() {
		return version;
	}
	public void setVersion(Integer version) {
		this.version = version;
	}

	/**
	 * Implementation of the equals method provided by the serializable interface.
	 */
	public boolean equals(Object other) {
        if (this == other) 
        	return true;
        if ( !(other instanceof ProductStockOT) ) 
        	return false;

        final ProductStockOT w = (ProductStockOT) other;

        return (this.warehouse == null?w.warehouse==null:this.warehouse.equals(w.warehouse)) &&
        	   (this.lot == null?w.lot==null:this.lot.equals(w.lot)) &&
        	   (this.stock == null?w.stock==null:this.stock.equals(w.stock));
    }

	/**
	 * Implementation of the hashCode method provided by the serializable interface.
	 */
    public int hashCode() {
        int result;
        if (this == null || this.getLot() == null || this.getWarehouse() == null || this.stock == null)
        	result = 0;
        else
        	result = this.getLot().hashCode() + this.getWarehouse().hashCode() + this.stock.hashCode();
        return result;
    }
    
	/**
	 * Implementation of the toString() method to show the data of the ProductOT
	 * formatted.
	 */
	public String toString() {
		StringBuffer result = new StringBuffer();
		try {
			result.append("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
			result.append("\nID: ");
			result.append(this.idStock);
			result.append("\nSTOCK: ");
			result.append(Converter.getInstance().toString(this.stock));
			result.append("\nLAST UPDATE: ");
			result.append(Converter.getInstance().toString(this.lastUpdate));
			result.append("\nLOT: ");
			result.append(this.lot.getInternalValue());
			result.append("\nWAREHOUSE: ");
			result.append(this.warehouse.getInternalValue());
			result.append("\nPRODUCT: ");
			result.append(this.product.getInternalValue());
			result.append("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
						
		}
		catch (Exception ex) {
			System.out.println("Error formatting ProductStockOT!");
			ex.printStackTrace();
			return null;
		}
		
		return result.toString();
	}
	
	@Override
	public boolean equals(Long id) {
		return id != null && this.idStock == id.longValue();
	}

	@Override
	public String getEntityId() {
		return InterfaceWarehouse.EXPLICA_ENTITY_WAREHOUSE_ID;
	}
	@Override
	public long getInternalId() {
		return this.getIdStock();
	}
	@Override
	public String getInternalValue() {
		if (this.lot != null && this.warehouse != null && this.stock != null)
			return String.format("[%s - %s] : %s", 
					this.lot.getInternalValue(), 
					this.warehouse.getInternalValue(),
					Converter.getInstance().toString(this.stock));
		else
			return "";
	}
	
	public String getLotDescription() {
		if (this.getLot() != null) {
			return this.getLot().getInternalValue();
		}
		else
			return "";
	}
	
	public String getWarehouseDescription() {
		if (this.getWarehouse() != null) {
			return this.getWarehouse().getInternalValue();
		}
		else
			return "";
	}
	
	public String getProductDescription() {
		if (this.getLot() != null)
			return this.getLot().getProduct().getDescription();
		else
			return "";
	}
	
	public String getLotNumber() {
		if (this.getLot() != null)
			return this.getLot().getNumber();
		else
			return "";
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: ProductStockOT.java,v 1.4 2009-10-07 12:10:30 mario Exp $";
	}
}
